package com.demon.auto.controller;

import com.demon.auto.model.ApiResponseModel;
import com.demon.auto.model.dto.UserDto;
import com.demon.auto.model.entity.UserEntity;
import com.demon.auto.model.vo.PageVo;
import com.demon.auto.model.vo.UserVo;
import com.demon.auto.service.UserService;
import com.demon.auto.utils.CommonUtil;
import com.demon.auto.utils.EmailUtils;
import com.demon.auto.utils.VerificationCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户信息
 *
 * @version 1.0
 * @author huangrz
 * @date 2025/02/14 16:42
 */
@RequestMapping("/api/user")
@RestController
public class UserController {

    @Autowired
    private UserService service;

    @Autowired
    private VerificationCodeUtil verificationCodeUtil;

    @Autowired
    private EmailUtils emailUtils;

    /**
     * 保存用户信息
     *
     * @param user 用户信息
     * @return ApiResponseModel<Boolean>
     */
    @RequestMapping("/save")
    public ApiResponseModel<Boolean> save(@Valid @RequestBody UserDto user) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(user, userEntity);
        Boolean flag = service.saveUserInfo(userEntity);
        if (flag == null) {
            return ApiResponseModel.error("用户名已存在！", false);
        }
        return ApiResponseModel.ok("保存用户信息", flag);
    }

    /**
     * 作废用户
     *
     * @param userId 用户ID
     * @return ApiResponseModel<Boolean>
     */
    @RequestMapping("/invalid")
    public ApiResponseModel<Boolean> invalid(Long userId) {
        if (userId == null) {
            return ApiResponseModel.error("用户ID不能为空", false);
        }
        return ApiResponseModel.ok("修改用户有效标识", service.removeById(userId));
    }

    @RequestMapping("/queryByCondition")
    public ApiResponseModel<PageVo<UserEntity>> queryByCondition(@RequestBody UserVo condition) {
        return ApiResponseModel.ok("查询用户信息成功！", service.queryByPage(condition));
    }

    @RequestMapping("/sendVerificationCode")
    public ApiResponseModel<Boolean> sendVerificationCode() {
        String email = CommonUtil.getCurrentUserEmail();
        if (StringUtils.isBlank(email)) {
            return ApiResponseModel.error("当前用户邮箱信息为空！", false);
        }
        if (!verificationCodeUtil.canSend()) {
            return ApiResponseModel.error("发送验证码过于频繁", false);
        }
        String code = VerificationCodeUtil.generateCode(6);
        if (!emailUtils.sendVerificationCodeEmail(email, code)) {
            return ApiResponseModel.error("发送验证码失败", false);
        }
        verificationCodeUtil.saveCode(code);
        return ApiResponseModel.ok("发送验证码成功", true);
    }

    @RequestMapping("/updatePassword")
    public ApiResponseModel<Boolean> updatePassword(String code, String password) {
        if (StringUtils.isBlank(password)) {
            return ApiResponseModel.error("密码不能为空！", false);
        }
        String savedCode = verificationCodeUtil.getCode();
        if (savedCode == null || !savedCode.equals(code)) {
            return ApiResponseModel.error("验证码错误或已过期", false);
        }
        if (!service.updatePassword(password)) {
            return ApiResponseModel.error("修改密码失败", false);
        }
        verificationCodeUtil.deleteCode();
        return ApiResponseModel.ok("修改密码成功", true);
    }

    @RequestMapping("/updateEmail")
    public ApiResponseModel<Boolean> updateEmail(String code, String email) {
        if (StringUtils.isBlank(email)) {
            return ApiResponseModel.error("邮箱不能为空！", false);
        }
        String savedCode = verificationCodeUtil.getCode();
        if (savedCode == null || !savedCode.equals(code)) {
            return ApiResponseModel.error("验证码错误或已过期", false);
        }
        if (!service.updateEmail(email)) {
            return ApiResponseModel.error("修改邮箱失败", false);
        }
        verificationCodeUtil.deleteCode();
        return ApiResponseModel.ok("修改邮箱成功", true);
    }

}
